﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace GameProject
{
    class Enemy
    {
        Random rng = new Random();
        public List<Enemy> enemyList;
        private Texture2D enemySprite;
        private Vector2 spriteStartPosition;
        private Vector2 enemySpritePosition;
        private Rectangle enemyHitBox;
        private Vector2 direction;
        private Vector2 goalCoorinates;
        private int maxEnemies;
        private SpriteFont spriteFont;
        private int enemySpawnCoolDown;
        private int enemySpawnTime;
        private int killCounter;
        private float movementSpeed;
        private Vector2 temp;
        private float health;
        private Vector2[] spawnPoints = 
        {
            new Vector2(-20, 365), 
            new Vector2(1021, 365),
            new Vector2(-20, 485),
            new Vector2(-20, 545),
            new Vector2(-20, 605),
            new Vector2(425, -20),
            new Vector2(1021, 425),
            new Vector2(365, -20),
            new Vector2(605, -20),
            new Vector2(-20, 425),
            new Vector2(485, -20),
            new Vector2(545, -20),
            new Vector2(1021, 545),
            new Vector2(1021, 485),
            new Vector2(1021, 605)
        };
        

        public Enemy(){ }
        public void Initialize()
        {
            movementSpeed = .25f;
            enemySpawnTime = 80;
            killCounter = 0;
            maxEnemies = 100;
            temp = new Vector2(0,1);
        }
        public void LoadContent(ContentManager Content)
        {
            enemyList = new List<Enemy>();
            enemySprite = Content.Load<Texture2D>("enemySprite");
            spriteFont = Content.Load<SpriteFont>("SpriteFont1");
        }
        public void Update(GameTime gameTime, Monument monument, Player player)
        {
            MouseState mouse = Mouse.GetState();
            Rectangle monumentHitBox = monument.getHitBox();
           
            if (enemySpawnCoolDown == 0)
            {
                if (enemyList.Count < maxEnemies)
                {
                    Enemy enemy = new Enemy();
                    enemy.health = rng.Next(10, 31);
                    enemy.goalCoorinates = monument.getMonumentMiddle();
                    enemy.enemySpritePosition = spawnPoints[rng.Next(spawnPoints.Length-1)];
                    enemy.spriteStartPosition = enemySpritePosition;
                    enemy.direction = enemy.goalCoorinates - enemy.enemySpritePosition;
                    enemy.enemyHitBox = new Rectangle((int)enemy.enemySpritePosition.X, (int)enemy.enemySpritePosition.Y, enemySprite.Width, enemySprite.Height);
                    enemyList.Add(enemy);
                    if(enemy.direction != Vector2.Zero)
                    {
                        direction.Normalize();
                    }
                }
                enemySpawnCoolDown = enemySpawnTime;
            }
            else
            {
                enemySpawnCoolDown--;
            }
            
            
            for (int i = 0; i < enemyList.Count; i++)
            {
                if(enemyList[i].enemyHitBox.Intersects(monumentHitBox))
                {
                    enemyList.RemoveAt(i);
                    monument.damageToHealth(-1);
                    killCounter++;
                }
                else if(enemyList[i].health <= 0)
                {
                    enemyList.RemoveAt(i);
                    player.addPoints();
                    killCounter++;
                }
                else
                {
                    enemyList[i].enemySpritePosition += enemyList[i].direction * movementSpeed*(float)gameTime.ElapsedGameTime.TotalSeconds;
                    enemyList[i].enemyHitBox = new Rectangle((int)enemyList[i].enemySpritePosition.X, (int)enemyList[i].enemySpritePosition.Y, enemySprite.Width, enemySprite.Height);
                }   
            }
            if(killCounter == 15 && enemySpawnTime > 15)
            {
                enemySpawnTime -= 2;
                killCounter = 0;
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < enemyList.Count; i++)
            {
                spriteBatch.Draw(enemySprite, enemyList[i].enemySpritePosition, Color.White);
            }
        }
        public Rectangle getEnemyHitBox()
        {
            return this.enemyHitBox;
        }
        public void damageHealth(int damage)
        {
            this.health -= damage;
        }
    }
}
